<?php


class Textmenu_IndexController extends WSB_Xtension_Navigation
{
	protected $_noViewRenderer = true;

	private $_html = '';
	private $_htmlPerLevel = array();
	private $_configuration;

	public function indexAction()
	{
		$this->_configuration = Zend_Registry::get('Zend_Config');
		if(isset($this->_config)) {
			$this->_walkThroughPageNode(WSB_Node_Page::getTree());
			foreach($this->_htmlPerLevel AS $levelNr=>$htmlPerLevel) {
				$levelKey = 'level' . $levelNr;
				// Level wrap
				if(isset($this->_config->$levelKey->wrap)) {
					$this->_html .= $this->_wrap($htmlPerLevel, $this->_config->$levelKey->wrap);
				} else {
					$this->_html .= $htmlPerLevel;
				}
			}
			// Menu wrap
			if(isset($this->_config->wrap)) {
				$this->_html = $this->_wrap($this->_html, $this->_config->wrap);
			}
		}
		$this->addContent($this->_html);
	}

	private function _walkThroughPageNode(WSB_Node_Page $node)
	{
		// Der Rootnode wird nicht beachtet!
		foreach($node->getChildren() AS $child) {
			$level = $child->getLevel();
			if(!isset($this->_htmlPerLevel[$level])) {
				$this->_htmlPerLevel[$level] = '';
			}
			if($level >= $this->_levelEntry) {
				$levelKey = 'level'.$level;
				// Im xml ist der entsprechende Level gesetzt
				if(isset($this->_config->$levelKey)) {
					// Status (current, active, normal)
					if($this->_id == $child->getPageId()) {
						$state = 'current';
					}
					else if($this->_inRootline($child->getPageId())) {
						$state = 'active';
					} else {
						$state = 'normal';
					}

					// Versteckt, Nicht im Menu
					if($child->hidden == 0 && $child->nav_hide == 0) {
						// Menuitem
						if(isset($this->_config->$levelKey->$state->doNotLinkIt) && (intval($this->_config->$levelKey->$state->doNotLinkIt) == 1)) {
							$item = $child->title;
						} else {
							$item = sprintf(
								'<a %3$shref="%1$s">%2$s</a>',
								$this->_getHref($child->getPageId()),
								$child->title,
								isset($this->_config->$levelKey->$state->ATagParams) ? $this->_config->$levelKey->$state->ATagParams : ''
							);

						}

						// Item status wrap
						if(isset($this->_config->$levelKey->$state->wrap)) {
							$item = $this->_wrap($item, $this->_config->$levelKey->$state->wrap);
						}
						$this->_htmlPerLevel[$level] .= $item;
					}

				}
			}
			if($child->hasChildren()) {
				if(in_array($child->getPageId(), self::$_rootline)) {
					$this->_walkThroughPageNode($child);
				}
			}
		}
	}

	private function _getHref($id)
	{
		$url = WSB_Xtension_Url::getInstance();
		return $url->getHrefToPage($id);
	}

}
